package com.example.wanxiang.service.Impl;

import com.example.wanxiang.dto.ArticleDto;
import com.example.wanxiang.entity.Article;
import com.example.wanxiang.repository.ArticleRepository;
import com.example.wanxiang.service.ArticleService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ArticleImpl implements ArticleService {

    @Resource
    private ArticleRepository articleRepository;

    @Override
    public List<Article> findByTitle(String title) {
        return articleRepository.findByTitleLike("%" + title + "%");
    }

    @Override
    public List<Article> getArticleByTitle(ArticleDto articleDto) {
        PageRequest request = getRequest(articleDto);
        return getArticleList(articleRepository.getArticleByTitleLike(articleDto.getTitle(), request));
    }

    @Override
    public long getArticleCount(ArticleDto articleDto) {
        return articleRepository.countArticleByTitle(articleDto.getTitle());
    }

    @Override
    public Article findById(String id) {
        return articleRepository.findById(id).orElse(null);
    }

    @Override
    public List<Article> findAll() {
        return articleRepository.findAll();
    }

    @Override
    public Article save(Article article) {
        return articleRepository.save(article);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(String id) {
        articleRepository.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Article update(String id, Article article) {
        Article existingArticle = findById(id);
        if (existingArticle != null) {
            existingArticle.setTitle(article.getTitle());
            existingArticle.setContent(article.getContent());
            existingArticle.setAuthor(article.getAuthor());
            existingArticle.setType(article.getType());
            existingArticle.setUserId(article.getUserId());
            existingArticle.setAuthorCreatedTime(article.getAuthorCreatedTime());
            existingArticle.setVisits(article.getVisits());
            existingArticle.setState(article.getState());
            existingArticle.setImageIds(article.getImageIds());
        }
        assert existingArticle != null;
        return articleRepository.save(existingArticle);
    }

    public PageRequest getRequest(ArticleDto articleDto) {
        return PageRequest.of((int) articleDto.getPage(), (int) articleDto.getSize());
    }

    public List<Article> getArticleList(Page<Article> articles) {
        List<Article> articleList = new ArrayList<Article>();

        for (Article article : articles) {
            articleList.add(article);
        }
        return articleList;
    }

}


